﻿namespace CSharpPro._1027;


using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// 学生类，用于表示学生信息及相关行为
/// </summary>
public class Student
{
    // 私有字段
    private string _studentId;   // 学号（唯一标识）
    private string _name;        // 姓名
    private int _age;            // 年龄
    private string _className;   // 班级
    private Dictionary<string, double> _scores;  // 课程成绩（课程名-分数）

    // 公共属性（带数据验证）
    public string StudentId
    {
        get => _studentId;
        set
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("学号不能为空或空白", nameof(value));
            _studentId = value;
        }
    }

    public string Name
    {
        get => _name;
        set
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("姓名不能为空或空白", nameof(value));
            _name = value;
        }
    }

    public int Age
    {
        get => _age;
        set
        {
            if (value < 5 || value > 50)
                throw new ArgumentOutOfRangeException(nameof(value), "年龄必须在5-50岁之间");
            _age = value;
        }
    }

    public string ClassName
    {
        get => _className;
        set => _className = value ?? string.Empty;
    }

    // 只读属性：课程数量
    public int CourseCount => _scores.Count;

    // 只读属性：平均成绩
    public double AverageScore
    {
        get
        {
            if (_scores.Count == 0) return 0;
            return _scores.Values.Average();
        }
    }

    // 构造函数
    public Student(string studentId, string name, int age)
    {
        StudentId = studentId;
        Name = name;
        Age = age;
        _className = string.Empty;
        _scores = new Dictionary<string, double>();
    }

    public Student(string studentId, string name, int age, string className)
        : this(studentId, name, age)
    {
        ClassName = className;
    }

    // 方法：添加或更新课程成绩
    public void SetScore(string courseName, double score)
    {
        if (string.IsNullOrWhiteSpace(courseName))
            throw new ArgumentException("课程名不能为空或空白", nameof(courseName));
        if (score < 0 || score > 100)
            throw new ArgumentOutOfRangeException(nameof(score), "分数必须在0-100之间");

        if (_scores.ContainsKey(courseName))
            _scores[courseName] = score;
        else
            _scores.Add(courseName, score);
    }

    // 方法：获取指定课程成绩
    public double? GetScore(string courseName)
    {
        if (_scores.TryGetValue(courseName, out double score))
            return score;
        return null; // 课程不存在时返回null
    }

    // 方法：删除课程成绩
    public bool RemoveScore(string courseName)
    {
        return _scores.Remove(courseName);
    }

    // 方法：显示所有课程成绩
    public void ShowScores()
    {
        if (_scores.Count == 0)
        {
            Console.WriteLine($"{Name}暂无成绩记录");
            return;
        }

        Console.WriteLine($"{Name}的成绩列表：");
        foreach (var (course, score) in _scores)
        {
            Console.WriteLine($"- {course}：{score}分");
        }
        Console.WriteLine($"平均成绩：{AverageScore:F1}分");
    }

    // 方法：学生自我介绍
    public void Introduce()
    {
        Console.WriteLine($"大家好！我是{ClassName}的{Name}，今年{Age}岁，学号是{StudentId}。");
    }

    // 重写ToString方法
    public override string ToString()
    {
        return $"{StudentId} - {Name}（{ClassName}）";
    }
}